// $codepro.audit.disable packageNamingConvention, com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString
/** Import Packages */
package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Account;
import edu.gatech.team404.AccountType;
import edu.gatech.team404.dal.AccountDAL;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class Registration
 * @author Michael Saul
 * @version 1.0
 */
@WebServlet("/Registration")
public class Registration extends HttpServlet {

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final HttpSession session = request.getSession(true);
        final PrintWriter out = response.getWriter();
        final String statusTag = "accountCreationStatus";
        
        final AccountDAL dal = new AccountDAL();
        final String courseName = request.getParameter("courseName");
        
        try {
           final String username = request.getParameter("username");
           final String password1 = request.getParameter("password1");
           final String password2 = request.getParameter("password2");
           final String name = request.getParameter("name");
           final String email = request.getParameter("email");
           final String type = request.getParameter("type");
           AccountType acctType = AccountType.UNKNOWN;

           boolean isValid = true;
           
           // Validate data
           if (username.isEmpty() ||
        	   password1.isEmpty() ||
        	   password2.isEmpty() ||
        	   name.isEmpty() ||
        	   email.isEmpty() ||
        	   type.isEmpty()) {
        	   isValid = false;
        	   session.setAttribute(statusTag, StatusCode.ACCOUNT_CREATE_INVALID_DATA);
           }
           
           // Validate password
           if (!password1.equals(password2)) {
        	   isValid = false;
        	   session.setAttribute(statusTag, 
        			   StatusCode.ACCOUNT_CREATE_MISMATCHED_PASSWORD);
           }
           
           //Parse Account type
           if (isValid) {
	           if(type.equalsIgnoreCase("Admin")) {
	        	   acctType = AccountType.ADMIN;
	           }
	           else if(type.equalsIgnoreCase("Instructor")){
	        	   acctType = AccountType.INSTRUCTOR;
	           }
	           else if(type.equalsIgnoreCase("Student")){
	        	   acctType = AccountType.STUDENT;
	           }
	           else{
	        	   isValid = false;
	        	   session.setAttribute(statusTag, 
	        			   StatusCode.ACCOUNT_CREATE_INVALID_DATA);
	           }
           }
           
           //Create account
           if (isValid) {
	           final Account acct = new Account (username, 
	        		   							 password1, 
	        		   							 name, 
	        		   							 email, 
	        		   							 acctType);
	           
	           final boolean acctCheck = dal.addAccount(acct);
	           
	           if(acctCheck) {
	        	   session.setAttribute(statusTag, StatusCode.ACCOUNT_CREATE_SUCCESS);
	           }
	           else{
	        	   session.setAttribute(statusTag, StatusCode.ACCOUNT_CREATE_FAIL);
	           }
           }     
        }
        finally {
        	session.setAttribute("feed", StatusCode.CREATE.toString());
        	if (!courseName.equalsIgnoreCase("null")) {
        		response.sendRedirect("account.jsp?course=" 
            			+ URLEncoder.encode(courseName, "UTF-8"));
        	}
        	else {
        		response.sendRedirect("account.jsp");
        	}
        	dal.close();
        	out.close();
        }
    }

	/** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    } 

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    }
    
}
